package com.example.weblending.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.weblending.domain.Post;
import com.example.weblending.domain.User;
import com.example.weblending.exception.GlobalException;
import com.example.weblending.mapper.PostMapper;
import com.example.weblending.service.PostService;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;

/**
* @author Administrator
* @description 针对表【post】的数据库操作Service实现
* @createDate 2023-05-03 10:24:40
*/
@Service
public class LengdingProductServiceImpl extends ServiceImpl<PostMapper, Post>
    implements PostService{

    @Override
    public boolean create(Post post, User user) {
        post.setOid(user.getUid());
        post.setName(user.getName());
        post.setType(1);
        post.setUid(user.getUid());
        post.setCreateTime(new Timestamp(System.currentTimeMillis()));
        post.setState(-1);
        return save(post);
    }

    @Override
    public Page<Post> getList(long page, long limit, User user) {
        QueryWrapper<Post> wrapper=new QueryWrapper<>();
        wrapper.eq("type",1);
        wrapper.eq("oid",user.getUid());
        wrapper.orderByAsc("create_time");
        Page<Post> conditionPage=new Page<>(page,limit);
        baseMapper.selectPage(conditionPage,wrapper);
        return conditionPage;
    }

    @Override
    public Page<Post> getHistory(long page, long limit, User user) {
        QueryWrapper<Post> wrapper=new QueryWrapper<>();
        wrapper.eq("type",1);
        wrapper.eq("state",0);
        wrapper.orderByDesc("create_time");
        Page<Post> conditionPage=new Page<>(page,limit);
        baseMapper.selectPage(conditionPage,wrapper);
        return conditionPage;
    }

    @Override
    public Page<Post> getExamine(long page, long limit, User user) {
        QueryWrapper<Post> wrapper=new QueryWrapper<>();
        wrapper.eq("type",0);
        wrapper.eq("state",0);
        wrapper.orderByDesc("create_time");
        Page<Post> conditionPage=new Page<>(page,limit);
        baseMapper.selectPage(conditionPage,wrapper);
        return conditionPage;
    }

    @Override
    public Page<Post> getApprove(long page, long limit, User user) {
        QueryWrapper<Post> wrapper=new QueryWrapper<>();
        wrapper.eq("oid",user.getUid());
        wrapper.ne("uid",user.getUid());
        wrapper.ne("state",0);
        wrapper.eq("type",0);
        wrapper.orderByDesc("operate_time");
        Page<Post> conditionPage=new Page<>(page,limit);
        baseMapper.selectPage(conditionPage,wrapper);
        return conditionPage;
    }

    @Override
    public boolean setState(int id, int i, User user) {
        Post applyCondition=getById(id);
        applyCondition.setState(i);
        applyCondition.setOid(user.getUid());
        applyCondition.setOperateTime(new Timestamp(System.currentTimeMillis()));
        return updateById(applyCondition);
    }

    @Override
    public boolean delete(int[] list, User user) {
        for (int i:list) {
            Post a=this.getById(i);
            if(a.getUid()!=user.getUid()){
                throw new GlobalException(403,"您无权操作");
            }else{
                removeById(i);
            }
        }
        return true;
    }

    @Override
    public Page<Post> getShow(long page, long limit, User user) {
        QueryWrapper<Post> wrapper=new QueryWrapper<>();
        wrapper.eq("state",1);
        wrapper.eq("type",1);
        wrapper.orderByDesc("create_time");
        Page<Post> conditionPage=new Page<>(page,limit);
        baseMapper.selectPage(conditionPage,wrapper);
        return conditionPage;
    }

    @Override
    public Page<Post> getPrime(long page, long limit, User user) {
        QueryWrapper<Post> wrapper=new QueryWrapper<>();
        wrapper.eq("state",2);
        wrapper.orderByDesc("create_time");
        Page<Post> conditionPage=new Page<>(page,limit);
        baseMapper.selectPage(conditionPage,wrapper);
        return conditionPage;
    }
}




